package com.example.demo.controller;

import com.example.demo.pojo.Student;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@RestController
public class StudentController {

    @Autowired
    StudentService studentService;
    //成功
    @RequestMapping(value = "/findStudentByAge",method = RequestMethod.GET)
    public List<Student> findStudentByAge (int age){
        return studentService.findStudentByAge(age);
    }
    //不成功
    @RequestMapping(value = "/insertStudent",method = RequestMethod.GET)
    public  String  insertStudent(String name,int age,String sex,String hobby){
        Student student = new Student();
        student.setName(name);
        student.setAge(age);
        student.setSex(sex);
        student.setHobby(hobby);
      if (studentService.insertStudent(student)){
             return "添加成功";
      }
        return  "添加失败";
    }
    //成功
    @RequestMapping("/selectStudentAll")
    public List<Student> selectStudentAll(){
        return studentService.selectStudentAll();
    }

    //成功
    @RequestMapping("/deleteStudentById")
    public String deleteStudent(int id){
        if (studentService.deleteStudent(id)){
            return "删除成功" ;
        }
        return "删除失败";
    }

    @RequestMapping(value = "/updateStudent",method = RequestMethod.GET)
    public String updateStudent(int id,String name,int age,String sex,String hobby){
        Student student = new Student();
        student.setId(id);
        student.setName(name);
        student.setAge(age);
        student.setSex(sex);
        student.setHobby(hobby);

        if (studentService.updateStudent(student)){
            return "修改成功";
        }
        return "修改失败";
    }

}
